﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace RFPL
{
    public partial class CoachEdit : Form
    {
        public int clubId = 0;
        public int fk_country = 0;
        public int coachId = 0;
        private bool playerCoach = false;

        public CoachEdit(int _clubId,int _coachId)
        {
            InitializeComponent();
            clubId = _clubId;
            coachId = _coachId;

        }

        private void CoachEdit_Load(object sender, EventArgs e)
        {
            if (clubId!=0)
            UpdatePlayerList();

            if (coachId != 0)
            {
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = Global.connectionString;
                conn.Open();
                SqlCommand myCommand = conn.CreateCommand();
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.CommandText = "[isPlayerCoach]";
                myCommand.Parameters.Add("@id", SqlDbType.Int).Value = coachId;
                
                int res = Convert.ToInt32(myCommand.ExecuteScalar());
                
                conn.Close();

                if (res == 0)
                {
                    radioButton1.Checked = true;

                    conn = new SqlConnection();
                    conn.ConnectionString = Global.connectionString;
                    conn.Open();
                    myCommand = conn.CreateCommand();
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.CommandText = "[getPerson]";
                    myCommand.Parameters.Add("@id", SqlDbType.Int).Value = coachId;
                    SqlDataReader dataReader = myCommand.ExecuteReader();
                    if (dataReader.Read())
                    {
                        if (!DBNull.Value.Equals(dataReader[0]))
                        {
                            coachPhoto.Image = ImageUtils.ByteArrayToImage((byte[])dataReader[0]);
                        }

                        coachName.Text = Convert.ToString(dataReader["name"]);

                        if (!DBNull.Value.Equals(dataReader["date"]))
                        {
                            dtmCoach.Value = Convert.ToDateTime(dataReader["date"]);
                        }

                        if (!DBNull.Value.Equals(dataReader["fk_country"]))
                        {
                            fk_country = Convert.ToInt32(dataReader["fk_country"]);
                            UpdateCountry();
                        }
                    }
                    dataReader.Close();
                    conn.Close();
                }
                else 
                {
                    radioButton2.Checked = true;
                    
                    foreach (DataGridViewRow row in dgvPlayers.Rows) // select city of stadion in dgv
                    {
                        if (Convert.ToInt32(row.Cells["id"].Value) == coachId)
                        {
                            row.Selected = true;
                            break;
                        }
                    }
                }


            }

        }

        private void UpdatePlayerList()
        {

            dgvPlayers.Rows.Clear();

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = Global.connectionString;
            conn.Open();
            SqlCommand myCommand = conn.CreateCommand();
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.CommandText = "[getPlayersOfClubPreview]";
            myCommand.Parameters.Add("@id", SqlDbType.Int).Value = clubId;
            SqlDataReader dataReader = myCommand.ExecuteReader();
            while (dataReader.Read())
            {
                dgvPlayers.Rows.Add(dataReader["id"], dataReader["photo"], dataReader["name"]);
            }
            dataReader.Close();
            conn.Close();

            dgvPlayers.ClearSelection();

        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                playerCoach = false;
                coachGroup.Enabled = true;
                playCoachGroup.Enabled = false;
            }
            else
            {
                playerCoach = true;
                coachGroup.Enabled = false;
                playCoachGroup.Enabled = true;
            }

            setSaveEnable();

        }

        private void UpdateCountry() {

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = Global.connectionString;
            conn.Open();
            SqlCommand myCommand = conn.CreateCommand();
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.CommandText = "[getCountry]";
            myCommand.Parameters.Add("@id", SqlDbType.Int).Value = fk_country;
            SqlDataReader dataReader = myCommand.ExecuteReader();
            if (dataReader.Read())
            {
                if (!DBNull.Value.Equals(dataReader["flag"]))
                {
                    flag.Image = ImageUtils.ByteArrayToImage((byte[])dataReader["flag"]);
                }
                country.Text = Convert.ToString(dataReader["name"]);
            }
            dataReader.Close();
            conn.Close();
        
        }

        private void country_Click(object sender, EventArgs e)
        {
            CountrySelect cs = new CountrySelect();
            cs.ShowDialog();
            fk_country = cs.countryId;

            if (fk_country != 0) {

                UpdateCountry();

            }

        }

        private void setSaveEnable() {

            if (!playerCoach)
            {
                if (coachName.Text.Length > 0)
                    saveButton.Enabled = true;
                else
                    saveButton.Enabled = false;
            }
            else
            { 
                if (dgvPlayers.SelectedRows.Count > 0)
                    saveButton.Enabled = true;
                else
                    saveButton.Enabled = false;
            }
        

        }

        private void coachName_TextChanged(object sender, EventArgs e)
        {
            setSaveEnable();
        }

        private void dgvPlayers_SelectionChanged(object sender, EventArgs e)
        {
            setSaveEnable();
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            if (coachId == 0)
            {

                if (!playerCoach)
                {
                    SqlConnection conn = new SqlConnection();
                    conn.ConnectionString = Global.connectionString;
                    SqlCommand myCommand = conn.CreateCommand();
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.CommandText = "[addPerson]";

                    if (coachPhoto.Image != null)
                    {
                        byte[] fl = ImageUtils.ImageToByteArray(coachPhoto.Image);
                        myCommand.Parameters.Add("@photo", SqlDbType.VarBinary, fl.Length).Value = fl;
                    }
                    else
                    {
                        myCommand.Parameters.Add("@photo", SqlDbType.VarBinary, 0).Value = DBNull.Value;
                    }

                    myCommand.Parameters.Add("@name", SqlDbType.NVarChar, coachName.Text.Length).Value = Convert.ToString(coachName.Text);

                    myCommand.Parameters.Add("@date", SqlDbType.Date).Value = dtmCoach.Value;

                    if (fk_country != 0)
                    {
                        myCommand.Parameters.Add("@fk_country", SqlDbType.Int).Value = fk_country;
                    }
                    else
                    {
                        myCommand.Parameters.Add("@fk_country", SqlDbType.Int).Value = DBNull.Value;
                    }


                    conn.Open();
                    coachId = Convert.ToInt32(myCommand.ExecuteScalar());
                    conn.Close();
                }
                else
                {
                    coachId = Convert.ToInt32(dgvPlayers.SelectedRows[0].Cells["id"].Value);
                }

            }
            else {
                if (!playerCoach)
                {

                    SqlConnection conn = new SqlConnection();
                    conn.ConnectionString = Global.connectionString;
                    SqlCommand myCommand = conn.CreateCommand();
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.CommandText = "[editPerson]";

                    myCommand.Parameters.Add("@id", SqlDbType.Int).Value = coachId;

                    if (coachPhoto.Image != null)
                    {
                        byte[] fl = ImageUtils.ImageToByteArray(coachPhoto.Image);
                        myCommand.Parameters.Add("@photo", SqlDbType.VarBinary, fl.Length).Value = fl;
                    }
                    else
                    {
                        myCommand.Parameters.Add("@photo", SqlDbType.VarBinary, 0).Value = DBNull.Value;
                    }

                    myCommand.Parameters.Add("@name", SqlDbType.NVarChar, coachName.Text.Length).Value = Convert.ToString(coachName.Text);

                    myCommand.Parameters.Add("@date", SqlDbType.Date).Value = dtmCoach.Value;

                    if (fk_country != 0)
                    {
                        myCommand.Parameters.Add("@fk_country", SqlDbType.Int).Value = fk_country;
                    }
                    else
                    {
                        myCommand.Parameters.Add("@fk_country", SqlDbType.Int).Value = DBNull.Value;
                    }


                    conn.Open();
                    myCommand.ExecuteScalar();
                    conn.Close();

                }
                else 
                {
                    coachId = Convert.ToInt32(dgvPlayers.SelectedRows[0].Cells["id"].Value);
                }            
            }
            this.Close();
        }

        private void coachPhoto_Click(object sender, EventArgs e)
        {
            ImageUtils.setImageFromStream(coachPhoto);  
        }
    }
}
